mod models;
mod config;
mod utils;
mod routes;
mod repositories;
mod tests;
mod app_state;
mod app_error;

use app_state::{AppState, get_app_state};
use std::net::SocketAddr;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
use std::sync::Arc;

#[tokio::main]
async fn main() {
    tracing_subscriber::registry()
        .with(
            tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| {
                format!("{}=debug,tower_http=debug", env!("CARGO_CRATE_NAME")).into()
            }),
        )
        .with(tracing_subscriber::fmt::layer())
        .init();
    
    let app_state = get_app_state().await;
    let app_state = Arc::new(app_state);
    let server_addr = SocketAddr::from(([127, 0, 0, 1], app_state.app_config.server_port));

    let app = routes::init_routes(app_state);


    let listener = tokio::net::TcpListener::bind(server_addr).await.unwrap();
    tracing::info!("Listening on {}", server_addr);
    axum::serve(listener, app).await.unwrap();
}
